package hr.dao;

import hr.to.DepartmentTO;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
//싱글톤 패턴으로 작성
public class DepartmentDAO {
	private static DepartmentDAO instance = new DepartmentDAO();
	private SqlMapClient sqlMap;
	private DepartmentDAO(){
		try {
			Reader reader = 
					Resources.getResourceAsReader("hr/config/SqlMapConfig.xml");
			sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static DepartmentDAO getInstance(){
		return instance;
	}
	public Object insertDepartment(DepartmentTO dto) throws SQLException{
		return sqlMap.insert("insertDepartment", dto);
	}
	public int updateDepartmentById(DepartmentTO dto)  throws SQLException{
		return sqlMap.update("updateDepartmentById", dto);
	}
	public int deleteDepartmentById(String departmentId) throws SQLException{
		return sqlMap.delete("deleteDepartmentById", departmentId);
	}
	public int deleteDepartmentByLocation(String location) throws SQLException{
		return sqlMap.delete("deleteDepartmentByLocation", location);
	}
	public List selectDepartmentList() throws SQLException{
		return sqlMap.queryForList("selectDepartmentList");
	}
	public List selectDepartmentByLocation(String location)  throws SQLException{
		return sqlMap.queryForList("selectDepartmentByLocation", location);
	}
	public DepartmentTO selectDepartmentById(String departmentId) throws SQLException{
		return (DepartmentTO)sqlMap.queryForObject("selectDepartmentById", departmentId);
	}
	public static void main(String[] args) throws SQLException{
		DepartmentDAO dao = DepartmentDAO.getInstance();
		DepartmentTO dto = new DepartmentTO("D004", "자재부", "인천지점");
		//insert
//		dao.insertDepartment(dto);
		
//		update
//		dto.setLocation("광주지점");
//		dao.updateDepartmentById(dto);
		
		//delete
//		dao.deleteDepartmentById("D004");
		dao.deleteDepartmentByLocation("인천지점");
//		System.out.println(dao.selectDepartmentById("D0001"));
		System.out.println(dao.selectDepartmentByLocation("서울지점"));
		System.out.println("----------------------");
		List list = dao.selectDepartmentList();
		for(Object obj : list){
			System.out.println(obj);
		}
	}
}











